/**
 *
 * Copyright (c) 2012 NCS Corporation.
 *
 * Author: tangb
 * Created Date: May 30, 2012
 * 
 * 	version					author					remark
 * 	1.0						tangb				
 */
package com.ocgs.base.core.web.action;

import com.ocgs.base.core.util.ApplicationContextHolder;
import com.ocgs.pagination.IExtendedPaginatedList;
import com.ocgs.pagination.PaginatedListImpl;
import com.ocgs.pagination.page.PageContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;

@SuppressWarnings("serial")
public abstract class BaseAction extends ActionSupport implements IBaseAction {

	private static final Log log = LogFactory.getLog(BaseAction.class);

    public PaginatedListImpl paginatedListImpl;

    public PaginatedList paginatedList;

    public PaginatedListImpl getPaginatedListImpl() {
        return paginatedListImpl;
    }

    public void setPaginatedListImpl(PaginatedListImpl paginatedListImpl) {
        this.paginatedListImpl = paginatedListImpl;
    }

    public PaginatedList getPaginatedList() {
        return paginatedList;
    }

    public void setPaginatedList(PaginatedList paginatedList) {
        this.paginatedList = paginatedList;
    }

    public BaseAction() {
	}

	public <T> T getBean(String name, Class<T> requiredType) {
		return (T) ApplicationContextHolder.getBean(name, requiredType);
	}

	public static <T> T getBean(Class<T> requiredType) {
		return ApplicationContextHolder.getBean(requiredType.getName(), requiredType);
	}

	public ActionContext getContext() {
		return ActionContext.getContext();
	}

	public HttpServletRequest getRequest() {
		return (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	}

	public HttpServletResponse getResponse() {
		return (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
	}


	protected String getParamValueByForce(Object obj) {
		String val = getParamValue(obj);
		return val == null ? null : StringUtils.isBlank(val) ? null : val;
	}

	private String getParamValue(Object obj) {
		return (String) (obj == null ? null : obj.getClass().isArray() ? ((Object[]) obj)[0] : obj);
	}



	protected final boolean isReadonly() {
		return "true".equalsIgnoreCase(getRequest().getParameter("webReadonly"));
	}

	protected final String rewriteUrl(String url) {
		if (isReadonly())
			return url + "&webReadonly=true";
		else
			return url;
	}
    public PaginatedListImpl init(HttpServletRequest request,PaginatedListImpl paginatedList){

        String sortCriterion = request.getParameter(IExtendedPaginatedList.IRequestParameters.SORT);
        if(StringUtils.isEmpty(sortCriterion))
            sortCriterion = "on_created";
        paginatedList.setSortDirection(IExtendedPaginatedList.IRequestParameters.ASC.equals(request.getParameter(IExtendedPaginatedList.IRequestParameters.DIRECTION))? SortOrderEnum.ASCENDING : SortOrderEnum.DESCENDING);
        paginatedList.setSortCriterion(sortCriterion);
        int pageSize = IExtendedPaginatedList.DEFAULT_PAGE_SIZE; // Rows per page
        paginatedList.setPageSize(pageSize);
        String currentPage = request.getParameter(IExtendedPaginatedList.IRequestParameters.PAGE);
        if(currentPage != null){
            int index = Integer.parseInt(currentPage) - 1;
            paginatedList.setIndex(index);
        }else{
            paginatedList.setIndex(0);
            currentPage="0";
            PageContext.removeContext();
        }
        PageContext pageContext = PageContext.getPageContext();
        pageContext.setCurrentPage(Integer.parseInt(currentPage));
        pageContext.setPageSize(pageSize);
        pageContext.setSortCriterion(sortCriterion);
        String sortDirection = request.getParameter(IExtendedPaginatedList.IRequestParameters.DIRECTION);
        pageContext.setSortDirection(sortDirection==null?"desc":sortDirection);
        return paginatedList;
    }
}